﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Services;
using Microsoft.Reporting.WinForms;
using MarginTrading.Common;

namespace MarginTrading.Forms
{
    public partial class ClientsLoanAccount : Form
    {
        public ClientsLoanAccount()
        {
            InitializeComponent();
        }

        private string GetClientCode()
        {
            string clientCode = string.Empty;

            if (!String.IsNullOrEmpty(txtClientName.Text))
            {
                int startIndex = txtClientName.Text.LastIndexOf('(') + 1;
                int length = txtClientName.Text.LastIndexOf(')') - txtClientName.Text.LastIndexOf('(') - 1;
                if (length > 0)
                {
                    clientCode = txtClientName.Text.Substring(startIndex, length);
                }
            }
            else
            {
                string strClientCode = "{0}-{1}-{2}-{3}";
                clientCode = string.Format(strClientCode, txtClientIDPrefix.Text, txtClientID1.Text, txtClientID2.Text,
                    txtClientID3.Text);
            }

            return clientCode;
        }

        private void ClientsLoanAccount_Load(object sender, EventArgs e)
        {
            AutoCompleteStringCollection clientNames = ClientService.GetClientNameCollection();
            AutoCompleteStringCollection clientCode = ClientService.GetClientCodeACCollection();

            if (clientNames != null)
            {
                txtClientName.AutoCompleteCustomSource = clientNames;
            }
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            if (!String.IsNullOrEmpty(GetClientCode()))
            {
                LoadReport(GetClientCode(), dateTimePicker1.Value.Date, dateTimePicker2.Value.Date);
            }
            button1.Enabled = true;
        }

        private void LoadReport(string clientId, DateTime fromDate, DateTime toDate)
        {
            toDate = toDate.AddDays(1);
            toDate = toDate.AddSeconds(-1);

            Client client = ClientService.GetClientByCode(clientId);
            double cumLoanAccount = ReportServices.GetCumLoanAccount(clientId, fromDate);

            if (client == null)
            {
                client = ClientService.GetClientByCode(clientId.ToUpper());
                if (client == null)
                {
                    client = new Client();
                }
                else
                {
                    clientId = clientId.ToUpper();
                }
            }

            ReportDataSource ds = new ReportDataSource();
            ds.Name = "master";
            ds.Value = ReportServices.GetLoanAccountCollectionDataTableByClientIdAndDateRange(clientId, fromDate, toDate);


            this.reportViewer1.LocalReport.ReportPath = "ClientsLoanAccount.rdlc";
            this.reportViewer1.LocalReport.DataSources.Clear();
            this.reportViewer1.LocalReport.DataSources.Add(ds);

            Config AgentCodeConfig = ConfigServices.GetConfigByName("Agent Code");
            string AgentCode = "cts";
            if (AgentCodeConfig != null)
            {
                AgentCode = AgentCodeConfig.Value;
            }

            //TODO:Remove this 
            AgentCode = Reports.AGENT_CODE;

            this.reportViewer1.LocalReport.SetParameters(new ReportParameter[] {
                        new ReportParameter("ImageLink",AgentCode.ToLower()),
                        new ReportParameter("CompanyName",Reports.COMPANY_NAME),
                        new ReportParameter("ClientID",client.Code),
                        new ReportParameter("CumLoanAccount",cumLoanAccount.ToString()),
                        new ReportParameter("Name",string.Concat(client.Initials,
                                                            " ", client.Surname) ),
                        new ReportParameter("Address",string.Concat(client.Address_1," ",client.Address_2," ",client.Address_3," ",client.Address_4)),
                        new ReportParameter("AdvisorID",client.Advisor),
                        new ReportParameter("LoanAccountDate",toDate.ToString(DateFormat.DATE_FORMAT)),
                        new ReportParameter("ReportTitle",string.Format(
                                                            Reports.CLIENT_LOAN_ACCOUNT_TITLE,
                                                            fromDate.ToString(DateFormat.DATE_FORMAT),
                                                            toDate.ToString(DateFormat.DATE_FORMAT)))});

            this.reportViewer1.RefreshReport();
            reportViewer1.SetDisplayMode(DisplayMode.PrintLayout);
            reportViewer1.ZoomMode = ZoomMode.Percent;
            reportViewer1.ZoomPercent = 100;
        }

        private void txtClientName_TextChanged(object sender, EventArgs e)
        {
            txtClientIDPrefix.Text = string.Empty;
            txtClientID1.Text = string.Empty;
            txtClientID2.Text = string.Empty;
            txtClientID3.Text = string.Empty;
        }

        private void txtClientIDPrefix_TextChanged(object sender, EventArgs e)
        {
            txtClientName.Text = string.Empty;
            if (txtClientIDPrefix.Text.Length == 3)
            {
                txtClientID1.Focus();
            }
        }

        private void txtClientID1_TextChanged(object sender, EventArgs e)
        {
            txtClientName.Text = string.Empty;
            if (txtClientID1.Text.Length == 9)
            {
                txtClientID2.Focus();
            }
        }

        private void txtClientID2_TextChanged(object sender, EventArgs e)
        {
            txtClientName.Text = string.Empty;
            if (txtClientID2.Text.Length == 2)
            {
                txtClientID3.Focus();
            }
        }

        private void txtClientID3_TextChanged(object sender, EventArgs e)
        {
            txtClientName.Text = string.Empty;
        }
    }
}
